from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions

# 无头浏览器设置
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
# 规避检查
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])

bro = webdriver.Chrome(executable_path="./chromedriver.exe", chrome_options=chrome_options, options=options)

# ============================= #
#           爬取车销量
# ============================= #

# with open("data/brand.txt", "r", encoding="utf-8") as lines:
#     for line in lines:
# print(line.split(",")[0].split("/")[-2].replace("nb",""))
for year in range(2017, 2021):
    print(year)
    bro.get("https://price.pcauto.com.cn/top/sales/s1-t3-y" + str(year) + "-m12.html")
    trs = bro.find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")
    content = ""
    for tr in trs:
        list = []
        for td in tr.find_elements_by_tag_name("td"):
            list.append(td.text)
            if (td.get_attribute("class") == "col2 brand") or (td.get_attribute("class") == "col4 relBrand"):
                list.append(td.find_element_by_tag_name("a").get_attribute("href").split("/")[-2])
        if len(list) != 0:
            content = content + (",").join(list) + "\n"
    print(content)
    with open("data/top/mpv及商用车_top_" + str(year) + ".csv", 'w', encoding="utf-8") as f:
        f.write("rank,series,seriesId,officePrice,brand,brandId,salesNum,salesSum\n")
        f.write(content)
